/**
 * Created by admin on 2017/4/5.
 */
var app = new Vue({
    el: '#movies',
    data: {
        movies: []
    },
    filters: {
        dataFormate: function (value) {
            if (!value) {
                return "";
            }
            var date = new Date(value);
            var format = date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
            return format;
        }
    },
    methods: {
        edit: function (movie) {
            console.log(movie);
            editMovieApp.movie = movie;

            $('#editMovieModal').modal('show');
        },
        deleteMovie: function (id) {
            debugger;
            this.$http.delete("/rest/movie/deleteMovie/" + id).then(function (response) {
                if (response && response.data) {
                    var data = response.data;
                    if (data.header && data.header.resultCode === "0") {
                        init();
                    }
                }
            });
            console.log(id);
        }
    }
});

var addMovieApp = new Vue({
        el: '#addMovieModal',
        data: {
            movie: {},
            message: "",
            alterError: false,
            alterSuccess: false
        },
        computed: {
            classObject: function () {
                return {
                    "alter-error": this.alterError,
                    "alter-success": this.alterSuccess
                }
            }
        },
        methods: {
            addMovie: function () {
                this.$http.post('/rest/movie/addMovie', this.movie).then(function (response) {
                    if (response && response.data) {
                        var data = response.data;
                        this.message = data.header.resultMessage;
                        if (data.header && data.header.resultCode === "0") {
                            this.alterSuccess = true;
                            init();
                        }
                    }
                });
            },
            close: function () {
                init();

            }
        }
    })
    ;

var editMovieApp = new Vue({
    el: '#editMovieModal',
    data: {
        movie: {},
        message: "",
        alterError: false,
        alterSuccess: false
    },
    computed: {
        classObject: function () {
            return {
                "alter-error": this.alterError,
                "alter-success": this.alterSuccess
            }
        }
    },
    methods: {
        editMovie: function () {
            this.$http.post('/rest/movie/editMovie', this.movie).then(function (response) {
                if (response && response.data) {
                    var data = response.data;
                    this.message = data.header.resultMessage;
                    if (data.header && data.header.resultCode === "0") {
                        this.alterSuccess = true;
                        init();
                    }
                }
            });
        },
        close: function () {
            init();
        }
    }
});
var init = function () {
    Vue.http.get("/rest/movie/getAllMovies").then(function (response) {
        if (response.data) {
            app.movies = response.data;
        }
    }, function (response) {
        console.log(response.data);
    });
}
Vue.http.get("/rest/movie/getAllMovies").then(function (response) {
    if (response.data) {
        app.movies = response.data;
    }
}, function (response) {
    console.log(response.data);
});